// user的控制器
const $ = require('jquery')
import users from "@/users.ejs"
let axios = require("../api/index")

export default function (router) {
    // 重新渲染页面
    async function readUser(res, p = 1) {
        let baseURL = axios.defaults.baseURL
        let reslut = await axios.get("/users/getData", {
            params: {
                p
            }
        })
        let userData = reslut.data.resData
        let pagingNum = reslut.data.pagingNum

        res.render(users({ userData, baseURL, pagingNum }))
    }
    async function cb(res) {
        // 点击添加按钮弹出输入框
        $(".adduer").on("click", () => {
            $(".adduermodel").show()
            $(".mask").show()
        })
        //点击×隐藏
        $(".closebtn").on("click", () => {
            $(".adduermodel").hide()
            $(".mask").hide()
        })

        // 获取图片,然后通过内置类修改为base64格式然后添加到预览框中
        let imgFile = document.querySelector(".imgFile")
        imgFile.onchange = function () {
            let filerader = new FileReader();
            filerader.readAsDataURL(this.files[0]);
            filerader.onload = function () {

                //创建Img标签
                let newImg = new Image()
                newImg.src = filerader.result
                newImg.style.height = "100px"
                $(".imgshow").html("").append(newImg).css({ "overflow": "hidden" })
            }
        }
        //测试axios发送
        // $(".loginout").on('click', async () => {
        //     let result = await axios.get("/users/getData")
        //     console.log(result.data);
        // })

        // 添加注册信息提交到后端.以及文件的提交,需要用到内置类
        let btn = document.querySelector(".btn")

        btn.onclick = async function () {
            // 获取文本内容

            let username = $(".username").val()
            let pwd = $(".pwd").val()
            let respwd = $(".repeatpwd").val()
            // 向后端发送文件请求需要借用到内置类FromData()
            // 非空判断
            let imgData = imgFile.files[0]
            if (username == "" || pwd == "" || imgData == "") {
                console.log("不能为空");
                return
            }
            if (respwd !== pwd) {
                console.log("两次密码不相等");
                return
            }
            let from = new FormData()
            from.append("username", username)
            from.append("pwd", pwd)
            from.append("myimg", imgData)
            let result = await axios.post("/users/addUser", from)

            await readUser(res)
            await cb(res)
            //点击提交后隐藏输入框
            $(".adduermodel").hide()
            $(".mask").hide()
        }
        // 删除用户数据
        $(".delBtn").on("click", async function () {
            let resBUl = confirm("是否确定删除")
            let _id = $(this).attr("myId")
            if (resBUl) {
                let reslut = await axios.delete("/users/delUser", {
                    params: {
                        _id
                    }
                })
                if (reslut.data.statu === 1) {
                    await readUser(res)
                    await cb(res)
                }
            }

        })
        //点击页码时向后端请求新的数据
        $(".paging a").on("click", async function (e) {
            e.preventDefault()
            let p = $.trim($(this).text())
            await readUser(res, p)
            await cb(res)
        })
        // 退出登录
        $(".loginout").on("click", function () {
            if (confirm("是否确定退出")) {
                // 跳转到login,然后清除localStorage
                router.go("/login")
                window.localStorage.removeItem('token')
            }
        })
    }
    router.route("/admin/users", async (req, res) => {
        await readUser(res)
        await cb(res)
    })
}